package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import custom.Activity2;
import custom.Question;
import custom.Quiz;
import custom.User;

/**
 * Servlet implementation class CompleteQuizCreationServlet
 */
@WebServlet("/CompleteQuizCreationServlet")
public class CompleteQuizCreationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CompleteQuizCreationServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("aeee 1");
		ArrayList<Question> list = (ArrayList<Question>) request.getSession().getAttribute("questions" );
		String desc = (String) request.getSession().getAttribute("quizName");
		String name = (String) request.getSession().getAttribute("quizDesc");
		User us = (User) request.getSession().getAttribute("user");
		
		RequestDispatcher dispatch= null;
		if(list.size()>0){
			Quiz qz = null;
			try {
				qz = Quiz.RegisterQuiz(name, us.getId(), desc, list);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			Activity2.registerActivity(us.getId(), qz.getId());
			dispatch = request.getRequestDispatcher("quizSummary.jsp?name=" + name);
			us.updateScore(2);
			request.getSession().removeAttribute("questions");
			request.getSession().removeAttribute("quizName");
			request.getSession().removeAttribute("quizDesc");
		}else{
			dispatch = request.getRequestDispatcher("editQuiz.jsp");
		}
		System.out.println("aeee 2");
		dispatch.forward(request, response);
	}

}
